@@ -48,40 +48,7 @@ static std::string extname(const std::string_view path) {
48
48
return std::string (name.substr (dot_pos + 1 ));
49
49
}
50
50
51
- int main (int argc, char *argv[]) {
52
-
53
- // process flags
54
- int opt;
55
- int infd = 0 ;
56
- int outfd = 1 ;
57
- const char *lang = nullptr ;
58
- const char *inpath = nullptr ;
59
- while ((opt = getopt (argc, argv, " hl:o:" )) != -1 ) {
60
- switch (opt) {
61
- case ' h' :
62
- printf (" usage: %s [-l LANG] [-o OUTFILE] [INFILE]\n " , argv[0 ]);
63
- exit (0 );
64
- case ' l' :
65
- lang = optarg ;
66
- break ;
67
- case ' o' :
68
- if ((outfd = creat (optarg , 0644 )) == -1 ) {
69
- perror (optarg );
70
- exit (1 );
71
- }
72
- break ;
73
- default :
74
- exit (1 );
75
- }
76
- }
77
- if (optind < argc) {
78
- inpath = argv[optind ];
79
- if ((infd = open (inpath, O_RDONLY)) == -1 ) {
80
- perror (inpath);
81
- exit (1 );
82
- }
83
- }
84
-
51
+ static void highlight (int infd, int outfd, const char *lang, const char *inpath) {
85
52
// create syntax highlighter
86
53
Highlight *h;
87
54
const char *ext;
@@ -123,3 +90,44 @@ int main(int argc, char *argv[]) {
123
90
H.flush (&res);
124
91
write (outfd, res.data (), res.size ());
125
92
}
93
+
94
+ int main (int argc, char *argv[]) {
95
+
96
+ // process flags
97
+ int opt;
98
+ int outfd = 1 ;
99
+ const char *lang = nullptr ;
100
+ while ((opt = getopt (argc, argv, " hl:o:" )) != -1 ) {
101
+ switch (opt) {
102
+ case ' h' :
103
+ printf (" usage: %s [-l LANG] [-o OUTFILE] [INFILE]\n " , argv[0 ]);
104
+ exit (0 );
105
+ case ' l' :
106
+ lang = optarg ;
107
+ break ;
108
+ case ' o' :
109
+ if ((outfd = creat (optarg , 0644 )) == -1 ) {
110
+ perror (optarg );
111
+ exit (1 );
112
+ }
113
+ break ;
114
+ default :
115
+ exit (1 );
116
+ }
117
+ }
118
+
119
+ // process files
120
+ if (optind == argc) {
121
+ highlight (0 , outfd, lang, 0 );
122
+ } else {
123
+ for (int i = optind ; i < argc; ++i) {
124
+ int infd;
125
+ const char *inpath = argv[i];
126
+ if ((infd = open (inpath, O_RDONLY)) == -1 ) {
127
+ perror (inpath);
128
+ exit (1 );
129
+ }
130
+ highlight (infd, outfd, lang, inpath);
131
+ }
132
+ }
133
+ }
0 commit comments